<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-07-31 16:01:34
 * @Description: 套餐卡产品详情
 */
class ServiceCardDetailPack extends Eloquent
{
    protected $table = "service_card_detail_pack";


     /**
     * @Author:      zwh
     * @DateTime:    2016-11-25 14:38:22
     * @Description: 用于订单结束时更改 套餐卡的使用状态
     */
    public static  function endOrder($order_id){
      return DB::transaction(function () use ($order_id) {
	    	ServiceCardConsumption::where("order_id",$order_id)->where("type",0)->where("status",4)->update(array("status"=>1));
	    });
    }


     /**
      * @Author:      lyt
      * @DateTime:  2017-08-03 10:53:26
      * @Description: 用于取消订单时候更改套餐卡使用状态返还套餐卡的
      */
    public static  function cancelOrder($order_id){
        return DB::transaction(function () use ($order_id) {
	    	$scc =ServiceCardConsumption::where("order_id",$order_id)->where("type",0)->where("status",4)->get();
	        foreach ($scc as $_scc) {
	        	DB::table("service_card_detail_pack")->where("id",$_scc->service_card_detail_pack_id)->increment("curr_number");
         	}      
         	ServiceCardConsumption::where("order_id",$order_id)->where("type",0)->where("status",4)->update(array("status"=>2));
	    });
    }

    /**
     * @Author:      dx
     * @DateTime:  2017-08-11 11:53:26
     * @Description: C端 支付完成生成相关套餐卡信息
     */
    public static  function createServiceCardDetail($order_id){
        return DB::transaction(function () use ($order_id) {

            $order=ServiceCardOrder::find($order_id);
            //生成套餐卡信息
            $scb=ServiceCardBase::find($order->service_card_id);
            $scbp=ServiceCardBasePack::where("service_card_base_id",$order->service_card_id)->get();
            $time=date("Y-m-d H:i:s",time()+$scb->date_num*24*60*60);

            $card_no = Common::createOrderId();
            $scd=new ServiceCardDetail();
            $scd->id=$card_no;
            $scd->service_card_base_id=$order->service_card_id;
            $scd->status=2;
            $scd->user_id=$order->user_id;
            $scd->get_time=Common::currentTimeString();
            $scd->end_date=$scb->date_type==1?$time:$scb->end_date;
            $scd->used_car_no=$order->car_no;
            $scd->shop_id=$order->shop_id;
            $scd->save();
            foreach ($scbp as $_scbp) {
                $scdp=new ServiceCardDetailPack();
                $scdp->service_card_detail_id=$card_no;
                $scdp->service_card_base_id=$order->service_card_id;
                $scdp->amount=$_scbp->amount;
                $scdp->curr_number=$_scbp->number;
                $scdp->number=$_scbp->number;
                $scdp->pack_id=$_scbp->pack_id;
                $scdp->created_by=$order->user_id;
                $scdp->save();
            }

            $sc_order_flows = new ServiceCardOrderFlow;
            $sc_order_flows->order_id = $order_id;
            $sc_order_flows->status =1417;
            $sc_order_flows->user_id = $order->user_id;
            $sc_order_flows->type = 'custom';
            $sc_order_flows->save();

            $order->service_card_detail_id=$card_no;
            $order->pay_time=date("Y-m-d H:i:s",time()+Config::get("config.report_time_diff"));
            $order->status=1417;
            $order->save();
        });

    }

}